/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vansante.EVEMap.UI;

import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import net.vansante.EVEMap.Data.EVEObjects;
import net.vansante.EVEMap.Data.Locations;
import net.vansante.EVEMap.Data.Solarsystem;
import net.vansante.EVEMap.Data.Station;
import net.vansante.EVEMap.Main;
import net.vansante.EVEMap.Map.MapControl;
import xmlparser.BuySellOrder;
import xmlparser.BuySellParser;
import xmlparser.OrderHandler;
import xmlparser.XMLparser;

/**
 *
 * @author Jon
 */
public class TradePanel extends JPanel {

    private String fileName;
    private XMLparser parser;
    private BuySellParser buySell;
    private OrderHandler activeHandler;
    /**
     * Creates new form TradePanel
     */
    public TradePanel() {
        initComponents();
        parser = new XMLparser();
        buySell = new BuySellParser();
        activeHandler = new OrderHandler();
    }
    
    private void loadFile(){
        itemLabel.setText("loading...");
        buySell = parser.readFile(fileName);
        itemLabel.setText(buySell.getItemName());
        if(buyRadio.isSelected()){
            activeHandler = buySell.getBuyOrders();
            MapControl.get().setSolarsystemMode(MapControl.SOLARSYSTEM_MODE_TRADE_BUY);
        }
        else{
            activeHandler = buySell.getSellOrders();
            MapControl.get().setSolarsystemMode(MapControl.SOLARSYSTEM_MODE_TRADE_SELL);
        }
        populateSolarSystems();
    }
    
    private void populateSolarSystems()
    {
        Locations solarsystems = Main.get().getSolarsystems();
        for(int i=0; i < solarsystems.size(); i++)
        {
            Solarsystem sol = (Solarsystem)solarsystems.get(i);
            sol.resetOrders();
        }
        
        List<BuySellOrder> orders = activeHandler.getOrders(); 
        for(int i=0; i < orders.size(); i++)
        {
            //get solarsystem via from global station list
            EVEObjects globalStations = Main.get().getStations();
            Station station = (Station)globalStations.getById(orders.get(i).station);
            Solarsystem sol = station.getSolarsystem();
            
            //add order to this solar system
            sol.addOrder(orders.get(i));
        }
    }
    
    public OrderHandler getActiveOrderHandler()
    {
        return activeHandler;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buySellGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        itemLabel = new javax.swing.JLabel();
        loadButton = new javax.swing.JButton();
        buyRadio = new javax.swing.JRadioButton();
        sellRadio = new javax.swing.JRadioButton();

        jLabel1.setText("Item:");

        itemLabel.setText("(ItemName)");

        loadButton.setText("Load New XML");
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        buySellGroup.add(buyRadio);
        buyRadio.setSelected(true);
        buyRadio.setText("Buy");
        buyRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioActionPerformed(evt);
            }
        });

        buySellGroup.add(sellRadio);
        sellRadio.setText("Sell");
        sellRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buyRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sellRadio)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(itemLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(loadButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(itemLabel)
                    .addComponent(loadButton))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buyRadio)
                    .addComponent(sellRadio))
                .addContainerGap(332, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        
        JFileChooser c = new JFileChooser();
      // Demonstrate "Save" dialog:
      int rVal = c.showOpenDialog(TradePanel.this);
      if (rVal == JFileChooser.APPROVE_OPTION) {
          fileName = c.getSelectedFile().getPath();
          loadFile();
      }
      if (rVal == JFileChooser.CANCEL_OPTION) {
          
      }
        
    }//GEN-LAST:event_loadButtonActionPerformed

    private void radioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioActionPerformed
        
        if("Buy".equals(evt.getActionCommand())){
            activeHandler = buySell.getBuyOrders();
            //TODO: add if for trade radio button in MapSettings
            MapControl.get().setSolarsystemMode(MapControl.SOLARSYSTEM_MODE_TRADE_BUY);
        }
        else{
            activeHandler = buySell.getSellOrders();
            MapControl.get().setSolarsystemMode(MapControl.SOLARSYSTEM_MODE_TRADE_SELL);
        }
        populateSolarSystems();
    }//GEN-LAST:event_radioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton buyRadio;
    private javax.swing.ButtonGroup buySellGroup;
    private javax.swing.JLabel itemLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton loadButton;
    private javax.swing.JRadioButton sellRadio;
    // End of variables declaration//GEN-END:variables
}
